package edu.upb.acs.jobsmaps.activity.search;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import edu.upb.acs.jobsmaps.R;
import edu.upb.acs.jobsmaps.activity.MainScreenActivity;
import edu.upb.acs.jobsmaps.activity.voiceRecognition.VoiceRecognitionListenerSearchJob;
import edu.upb.acs.jobsmaps.voice.VoiceUtils;

public class SearchJobActivity extends Activity implements TextToSpeech.OnInitListener {

    private static final String TAG = SearchJobActivity.class.getName();

	private SpeechRecognizer sr;
	private Intent intent;
	private TextToSpeech tts;
	private AudioManager amAudioManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_job);
        Log.d(TAG, "Initialize activity");



		DisplayMetrics metrics = getResources().getDisplayMetrics();
		int screenWidth = (int) (metrics.widthPixels * 0.80);
		getWindow().setLayout(screenWidth, LinearLayout.LayoutParams.WRAP_CONTENT);

		Button search = (Button) findViewById(R.id.searchButtonJob);
		search.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				final Spinner spinner = (Spinner) findViewById(R.id.searchCriteriaJob);
				final String searchCriteria = (String) spinner.getSelectedItem();

				final EditText searchWordField = (EditText) findViewById(R.id.searchWordJob);
				final String searchWord = searchWordField.getText().toString();

				final String profile = ((Spinner) findViewById(R.id.searchJobProfile)).getSelectedItem().toString();
				final String distance = ((Spinner) findViewById(R.id.searchJobDistance)).getSelectedItem().toString();

				Log.d(TAG, "Search after searchCriteria = " + searchCriteria + ", searchWord = " + searchWord
						+ " , profile = " + profile + " , distance = " + distance);

				Intent i = new Intent(SearchJobActivity.this,
						MainScreenActivity.class);
				i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				i.putExtra("search_word_job", searchWord);
				i.putExtra("search_criteria_job", searchCriteria);
				i.putExtra("search_distance_job", distance);
				i.putExtra("search_profile_job", profile);

				startActivity(i);
			}
		});

		//the audio manager for microphone
		amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

		//create a new speaker
		tts = new TextToSpeech(this, this);

		Log.d(TAG, "Entering startListening");
		startListening();
    }

	private void startListening() {
		sr = SpeechRecognizer.createSpeechRecognizer(this);

		intent= VoiceUtils.initializeIndent(intent, getApplicationContext());

		sr.setRecognitionListener(new VoiceRecognitionListenerSearchJob(sr, intent, tts, amAudioManager, this));
		sr.startListening(intent);
	}

	@Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "Start activity");


    }

	@Override
	public void onDestroy() {
		// Don't forget to shutdown tts!
		if (tts != null) {
			tts.stop();
			tts.shutdown();
		}
		super.onDestroy();
	}
	//folosit la TextToSpeech
	@Override
	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			//Speak
			tts.speak("Search criteria", TextToSpeech.QUEUE_FLUSH, null);
			while(tts.isSpeaking())
			{
				//Log.d(TAG, "She is speaking");
			}
		}
		else {
			//Handle initialization error here
			Log.d(TAG, "Initialization for TTS failed!!!");
		}
	}
}
